/****************************************************************************
 * Copyright (C) 2012
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 ***************************************************************************/
#include "CustomPathsSM.hpp"
#include "../../FileOperations/fileops.h"
#include "../../Controls/Application.h"
#include "../../Prompts/PromptWindows.h"
#include "../../Themes/CTheme.h"
#include "../../Menu/Explorer.h"
#include "../../Menu/TaskBar.hpp"

CustomPathsSM::CustomPathsSM()
	: SettingsBrowser(), setPath(0), setPathSize(0)
{
	EmuNandPathSetting_1 = Settings.EmuNandPath_1;
	EmuNandPathSetting_2 = Settings.EmuNandPath_2;
	EmuNandPathSetting_3 = Settings.EmuNandPath_3;
	GameTDBPathSetting = Settings.GameTDBPath;
	
	SetupOptionName();
	show();
}

CustomPathsSM::~CustomPathsSM()
{
	bool reload = false;
	
	if (GameTDBPathSetting.compare(Settings.GameTDBPath) &&
		Settings.GameTDBTitles &&
		Settings.LoadedBrowser != MII_BROWSER &&
		Settings.LoadedBrowser != EMII_BROWSER)
	{
		reload = true;
	}
	
	if (EmuNandPathSetting_1.compare(Settings.EmuNandPath_1) ||
		EmuNandPathSetting_2.compare(Settings.EmuNandPath_2) ||
		EmuNandPathSetting_3.compare(Settings.EmuNandPath_3))
	{
		FixEMiiDB();
		
		if (Settings.LoadedBrowser == ESAVE_BROWSER ||
			Settings.LoadedBrowser == EMII_BROWSER)
		{
			if ((EmuNandPathSetting_1.compare(Settings.EmuNandPath_1) && !EmuNandPathSetting_1.compare(CurrentBrowser()->GetCurrentPath())) ||
				(EmuNandPathSetting_2.compare(Settings.EmuNandPath_2) && !EmuNandPathSetting_2.compare(CurrentBrowser()->GetCurrentPath())) ||
				(EmuNandPathSetting_3.compare(Settings.EmuNandPath_3) && !EmuNandPathSetting_3.compare(CurrentBrowser()->GetCurrentPath())))
				reload = true;
		}
	}
	
	if(reload)
	{
		Explorer::Instance()->Reload();
		TaskBar::Instance()->UpdateAllButtons();
	}
}

void CustomPathsSM::SetupOptionName()
{
	int Idx = 0;
	
	options->SetName(Idx++, tr("Saves Path"));
	options->SetName(Idx++, tr("Update Path"));
	options->SetName(Idx++, tr("Custom Font"));
	options->SetName(Idx++, tr("Screenshot Path"));
	options->SetName(Idx++, tr("Theme Path"));
	options->SetName(Idx++, tr("Homebrew Path"));
	options->SetName(Idx++, tr("Miis Path"));
	options->SetName(Idx++, tr("GameTDB Path"));
	options->SetName(Idx++, tr("Data.bin Path"));
	if(!Settings.SneekRunning)
	{
		options->SetName(Idx++, "%s%s", tr("Emu Nand Path"), " 1");
		options->SetName(Idx++, "%s%s", tr("Emu Nand Path"), " 2");
		options->SetName(Idx++, "%s%s", tr("Emu Nand Path"), " 3");
	}
	
	SetOptionValues();
}

void CustomPathsSM::SetOptionValues()
{
	int Idx = 0;
	
	//! Settings: Saves Path
	options->SetValue(Idx++, Settings.SaveGamePath);
	
	//! Settings: Update Path
	options->SetValue(Idx++, Settings.UpdatePath);
	
	//! Settings: Custom Font
	options->SetValue(Idx++, Settings.UseSystemFont ? tr( "Disabled" ) : Settings.CustomFontPath);
	
	//! Settings: Screenshot Path
	options->SetValue(Idx++, Settings.ScreenshotPath);
	
	//! Settings: Theme Path
	options->SetValue(Idx++, Settings.ThemePath);
	
	//! Settings: Homebrew Path
	options->SetValue(Idx++, Settings.HomebrewPath);
	
	//! Settings: Miis Path
	options->SetValue(Idx++, Settings.SaveMiisPath);
	
	//! Settings: GameTDB Path
	options->SetValue(Idx++, Settings.GameTDBPath);
	
	//! Settings: Data.bin Path
	options->SetValue(Idx++, Settings.DataBinPath);
	
	//! Settings: Emu Nand Path
	if(!Settings.SneekRunning)
	{
		options->SetValue(Idx++, Settings.EmuNandPath_1);
		options->SetValue(Idx++, Settings.EmuNandPath_2);
		options->SetValue(Idx++, Settings.EmuNandPath_3);
	}
}

void CustomPathsSM::OnBrowserClick(GuiOptionBrowser * browser UNUSED, int option)
{
	switch (option)
	{
		case 0:
			ChangePath(Settings.SaveGamePath, sizeof(Settings.SaveGamePath));
			break;
		case 1:
			ChangePath(Settings.UpdatePath, sizeof(Settings.UpdatePath));
			break;
		case 2:
			if(!Settings.UseSystemFont)
				ChangePath(Settings.CustomFontPath, sizeof(Settings.CustomFontPath), FILTER_FONT);
			break;
		case 3:
			ChangePath(Settings.ScreenshotPath, sizeof(Settings.ScreenshotPath));
			break;
		case 4:
			ChangePath(Settings.ThemePath, sizeof(Settings.ThemePath));
			break;
		case 5:
			ChangePath(Settings.HomebrewPath, sizeof(Settings.HomebrewPath));
			break;
		case 6:
			ChangePath(Settings.SaveMiisPath, sizeof(Settings.SaveMiisPath));
			break;
		case 7:
			ChangePath(Settings.GameTDBPath, sizeof(Settings.GameTDBPath));
			break;
		case 8:
			ChangePath(Settings.DataBinPath, sizeof(Settings.DataBinPath));
			break;
		case 9:
			ChangePath(Settings.EmuNandPath_1, sizeof(Settings.EmuNandPath_1));
			break;
		case 10:
			ChangePath(Settings.EmuNandPath_2, sizeof(Settings.EmuNandPath_2));
			break;
		case 11:
			ChangePath(Settings.EmuNandPath_3, sizeof(Settings.EmuNandPath_3));
			break;
		default:
			break;
	}
	
	SetOptionValues();
	optionBrowser->Refresh();
}

void CustomPathsSM::ChangePath(char *path, int size, u8 filter)
{
	SetEffect(EFFECT_FADE, -20);
	while(this->IsAnimated())
		Application::Instance()->updateEvents();
	
	setPath = path;
	setPathSize = size;
	
	PathBrowser *pathBrowser = new PathBrowser(path, filter);
	pathBrowser->ButtonClick.connect(this, &CustomPathsSM::OnPathBrowserButtonClick);
}

void CustomPathsSM::OnPathBrowserButtonClick(PathBrowser *menu, u8 filter, bool doneClicked, const std::string &Path)
{
	menu->SetEffect(EFFECT_FADE, -20);
	while(menu->IsAnimated())
		Application::Instance()->updateEvents();
	menu->Close();
	
	if(doneClicked)
	{
		if (Path.size() && (filter & FILTER_PATH))
		{
			std::string tmp = Path;
			while((tmp[tmp.size()-1] == '/') && (tmp[tmp.size()-2] != ':'))
				tmp.erase(tmp.size()-1);
			
			snprintf(setPath, setPathSize, "%s", tmp.c_str());
		}
		else if (Path.size() && (filter & FILTER_FONT))
		{
			Application::Instance()->setReload(Application::RELOAD_FONT);
			
			snprintf(setPath, setPathSize, "%s", Path.c_str());
			Theme::LoadFont(Settings.CustomFontPath);
			
			Settings.Save();
			
			ThemeReloadRequest();
			return;
		}
	}

	SetOptionValues();
	optionBrowser->Refresh();
	
	SetEffect(EFFECT_FADE, 20);
}
